<?php

/**
 * 消息推送
 * @author chengx impl@wanthings.com
 */
class NotifyCommand extends CConsoleCommand
{


    /**
     * 群推
     * @param  [type] $bda     [description]
     * @param  [type] $bdi     [description]
     * @param  [type] $content [description]
     * @param  [type] $msg_id  [description]
     * @param  string $to [description]
     * @return [type]          [description]
     */
    private function _pushAll($bda, $bdi, $content, $msg_id, $to = 'zb')
    {
        $bda_rs = $bda->pushAll($content);
        $bdi_rs = $bdi->pushAll($content);
        if ($bda_rs && $bdi_rs) {
            Message::model()->changePushStatus($msg_id);
            echo 'push all success (' . $this->apps[$to] . ')' . PHP_EOL;
        } else {
            Yii::Log(CJSON::encode($bda_rs), 'error', 'baidu push all error (android)');
            Yii::Log(CJSON::encode($bdi_rs), 'error', 'baidu push all error (ios)');
            Message::model()->changePushStatus($msg_id, Message::PUSH_ERROR);
            echo 'push all faile (' . $this->apps[$to] . ')' . PHP_EOL;
        }
        sleep(4);
    }

    /**
     * 单推
     * @param  [type] $bd         [description]
     * @param  [type] $content    [description]
     * @param  [type] $channel_id [description]
     * @param  [type] $msg_id     [description]
     * @param  string $to [description]
     * @return [type]             [description]
     */
    private function _pushSingle($bd, $content, $channel_id, $msg_id, $to = 'zb')
    {
        $bd_rs = $bd->pushSingle($content, $channel_id);
        if ($bd_rs) {
            Message::model()->changePushStatus($msg_id);
            echo 'push to user success(' . $this->apps[$to] . ')' . PHP_EOL;
        } else {
            Yii::Log(CJSON::encode($bd_rs), 'error', 'baidu push error');
            Message::model()->changePushStatus($msg_id, Message::PUSH_ERROR);
            echo 'push to user faile(' . $this->apps[$to] . ')' . PHP_EOL;
        }
    }

    public function actionPush()
    {
        date_default_timezone_set('Asia/Shanghai');
        if (Yii::app()->cache->get('push_lock') === false) {
            Yii::app()->cache->set('push_lock', 1);
            Message::model()->pushMessage();//消息推送
            Yii::app()->cache->delete('push_lock');
        } else {
            echo 'running' . PHP_EOL;
        }
    }
}

?>
